<template>
  <a-modal
    v-if="visible"
    :visible="true"
    title="罚款事由"
    @cancel="cancel"
    :width="1100"
    @ok="ok"
  >
    <a-card>
      <title-name title="搜索" />
      <a-form-model class="ant-advanced-search-form">
        <a-row>
          <a-col span="8">
            <a-form-model-item label="所属分公司">
              <SelectSubCompany allow-clear v-model="queryParam.branchComId" :auto-selected="false" />
            </a-form-model-item>
          </a-col>
          <a-col span="8">
            <a-form-model-item label="所属项目">
              <SelectProject allow-clear v-model="queryParam.projectId" keyValue="branchComId" :form="queryParam"
                             :auto-selected="false"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="8" style="text-align: right">
            <a-button type="primary" html-type="submit" @click="search"> 搜索</a-button>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="8">
            <a-form-model-item label="检查时间">
              <a-range-picker format="YYYY-MM-DD" v-model="queryParam.createTime" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>
    <b-table
      ref="table"
      :columns="columns"
      :request="loadData"
      :selectConfig="{use: true, type: 'checkbox',rowKey: 'id' }"
      @select-click="selectClick"
    />
  </a-modal>
</template>
<script>
import LApi from '@/api/safetySupervision/checkAndRectify/checkRecord'
import moment from 'moment'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'

export default {
  name: 'addCheckRecord',
  components: {
    SelectProject,
    SelectSubCompany
  },
  props: {
    visible: false,
    isPerson: false,
    outSelectRows: {
      type: Array,
      default: []
    },
    outParams: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      columns: [
        { title: '分公司名称', dataIndex: 'branchComName', width: 140 },
        { title: '项目名称', ellipsis: true, dataIndex: 'projectName', width: 140 },
        { title: '检查区域', dataIndex: 'respAreaStr' },
        { title: '问题描述', dataIndex: 'problemDescription' },
        { title: '检查时间', dataIndex: 'createTime' },
        {
          title: '整改状态', dataIndex: 'safetyAuditStatusEnumStr', width: 130,
          customRender: (text, row, index) => {
            if (row.safetyAuditStatusEnumStr === '待整改' && row.isBeyond == '1') {
              return '超期未整改'
            }
            return row.safetyAuditStatusEnumStr
          }
        }
      ],
      queryParam: {
        branchComId: undefined,
        projectId: undefined,
        createTime: [moment(), moment()]
      },
      loadData: async p => {
        this.$console(this.outParams)
        const q = Object.assign({}, p, { query: { ...this.queryParam } })

        if (this.isPerson) {
          if (!q.query.projectId) return { code: this.SUCCESS_CODE, data: [] }
        }
        if (q.query.createTime && q.query.createTime.length) {
          q.query.createTimeStart = moment(q.query.createTime[0]).format('YYYY-MM-DD') + ' 00:00:00'
          q.query.createTimeEnd = moment(q.query.createTime[1]).format('YYYY-MM-DD') + ' 23:59:59'
        } else {
          q.query.createTimeStart = moment().format('YYYY-MM-DD') + ' 00:00:00'
          q.query.createTimeEnd = moment().format('YYYY-MM-DD') + ' 23:59:59'
        }
        q.query.safetyAuditStatusEnum = 'UNQUALIFEDS'
        let res = await LApi.findListByMainPc({}, q)
        if (res.code === this.SUCCESS_CODE) {
          res.data.map(v => {
            v.disabled = this.outSelectRows.findIndex(w => w.id === v.id) !== -1
          })
          return { code: this.SUCCESS_CODE, data: res.data, ...res }
        }
        return { code: this.SUCCESS_CODE, data: [] }
      },
      selectRows: []
    }
  },
  computed: {
    searchArr() {
      return [
        // {
        //   name: '所属分公司', valueKey: 'branchComId', type: SelectSubCompany,
        //   setInitValue: true,
        //   initValue: this.outParams.branchComId
        // },
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属项目', keyValue: 'branchComId', valueKey: 'projectId', type: SelectProject,
        //   setInitValue: true,
        //   initValue: this.outParams.projectId,
        //   orgId: this.outParams.branchComId,
        //   useOnceOrgId: true
        // },
        {
          name: '检查时间',
          value: 'createTime',
          type: 'dateRange',
          attr: {
            rangeFormat: ['createTimeStart', 'createTimeEnd'],
            allowClear: false
          },
          setInitValue: true,
          initValue: [moment(), moment()]
        }
      ]
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.selectRows = []
        this.$refs.table && this.$refs.table.refresh()
        this.queryParam = { ...this.queryParam, ...this.outParams }
      } else {
        this.queryParam = {
          branchComId: undefined,
          projectId: undefined,
          createTime: [moment(), moment()]
        }
      }
    }
  },
  created() {
  },
  methods: {
    search() {
      if (!this.queryParam.projectId && this.isPerson) {
        this.$noticeWarning('请选择项目')
      }
      this.$refs.table.refresh()
      this.selectRows = []
    },
    selectClick(e) {
      this.selectRows = e.rows
    },
    ok() {
      if (!this.selectRows.length) {
        return this.$noticeWarning('请选择数据')
      }
      this.$emit('ok', this.selectRows)
      this.cancel()
    },
    cancel() {
      this.$emit('update:visible', false)
    }
  }
}
</script>
